package com.dankegongyu.lib.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DKMd5Util {

    private DKMd5Util() {
        throw new AssertionError("cannot be instantiated");
    }

    public static String getMD5(String content) {
        if (StringUtil.isEmpty(content)) {
            return "";
        }
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(content.getBytes());
            return getHashString(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String getHashString(MessageDigest digest) {
        StringBuilder builder = new StringBuilder();
        for (byte b : digest.digest()) {
            builder.append(Integer.toHexString((b >> 4) & 0xf));
            builder.append(Integer.toHexString(b & 0xf));
        }
        return builder.toString();
    }

}
